<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <style>
      .box {
        width: 300px;
        height: 100px;
        background: plum;
        position: absolute;
        left: 50%;
        transform: translate(-50%);
        top: 100px;
      }
      .container {
        width: 100px;
        height: 200px;
        background: skyblue;
      }
    </style>
  </head>
  <body>
    <div class="container"></div>
    <div class="box"></div>
    <button class="btn">点击</button>
  </body>
</html>
<script>
  var boxEle = document.getElementsByClassName("box")[0];
  var btnEle = document.getElementsByClassName("btn")[0];
  window.onscroll = function () {
    if (top > 100) {
      boxEle.style.position = "fixed";
      boxEle.style.top = "100px";
    }
  };
  btnEle.onclick = function () {
    var top = document.documentElement.scorllTop;
    function setTop() {
      setTimeout(function setTop() {
        top -= 100;
        document.documentElement.scorllTop = top;
        if (top > 0) {
          setTop();
        }
      }, 5);
    }
    setTop();
  };
</script>
